package com.partners.utils;

import com.partners.model.Member;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static com.partners.configure.RedisConfigurtion.REDIS_TEMPLATE_BEAN_NAME;

/**
 * @author liangyongle
 * @date 2019-01-16
 */
@Component
public class TokenSession {

    @Resource(name = REDIS_TEMPLATE_BEAN_NAME)
    private  RedisTemplate<String, Serializable> REDIS_TEMPLATE;

    private  final String TOKEN_PREFIX = "token::";

    /**
     * session有效时长为30分钟
     */
    private  final Duration SESSION_DURATION = Duration.ofMinutes(30L);

    /**
     * 设置缓存
    * */
    public   void updateMemberTokenContent(String token, Member member) {
        REDIS_TEMPLATE.opsForValue().set(TOKEN_PREFIX+token,member,SESSION_DURATION.toMinutes(), TimeUnit.MINUTES);
    }

    /**
     * 获取缓存
     * */
    public  Member getMemberTokenContent(String token) {
        return (Member) REDIS_TEMPLATE.opsForValue().get(TOKEN_PREFIX+token);
    }
}
